﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;       // 地图图层相关接口（ILayer/ILegendClass）
using ESRI.ArcGIS.Display;     // 符号与显示相关接口（ISymbol/IColor）
using ESRI.ArcGIS.esriSystem;  // ArcGIS系统基础接口（IPropertySet等）
using ESRI.ArcGIS.SystemUI;    // 系统UI相关接口
using ESRI.ArcGIS.Controls;    // 控件相关接口（SymbologyControl）
using ESRI.ArcGIS.Geodatabase; // 地理数据库相关接口（IFeatureClass）



namespace MyMap
{
    public partial class SymbolSelectDialog : Form
    {
        private ILegendClass m_legendClass;       // 当前选中的图例类（用于更新符号）
        private ILayer m_layer;                   // 当前操作的图层（判断点/线/面类型）
        private IStyleGalleryItem m_styleGalleryItem; // 当前选中的符号样式项
        private ISymbol m_symbol;                 // 当前选中的符号对象
        private Image m_symbolImage;              // 符号预览图像

        public ISymbol currentSymbol()
        {
            return m_symbol;
        }


        public SymbolSelectDialog(ILegendClass tempLegendClass, ILayer tempLayer)
        {
            InitializeComponent(); // 初始化窗体控件
                                   // 赋值成员变量
            m_legendClass = tempLegendClass;
            m_layer = tempLayer;
            // 配置符号文件选择器（筛选*.ServerStyle文件）
            openFileDialog1.Filter = "ServerStyle符号文件 (*.ServerStyle)|*.ServerStyle|所有文件 (*.*)|*.*";
            openFileDialog1.Title = "选择自定义符号文件";
        }

        private void setFeatureClassStyle(esriSymbologyStyleClass tempSymbologyStyleClass)
        {
            this.axSymbologyControl.StyleClass = tempSymbologyStyleClass;
            ISymbologyStyleClass symbologyStyleClass = this.axSymbologyControl.GetStyleClass(tempSymbologyStyleClass);
            if (this.m_legendClass != null)
            {
                IStyleGalleryItem currentStyleGalleryItem = new ServerStyleGalleryItem();
                currentStyleGalleryItem.Name = "当前符号";
                currentStyleGalleryItem.Item = m_legendClass.Symbol;
                symbologyStyleClass.AddItem(currentStyleGalleryItem, 0);
                m_styleGalleryItem = currentStyleGalleryItem;
            }
            symbologyStyleClass.SelectItem(0);
        }


        // 根据图层类型为SymbolControl导入相应的符号样式文件，如点、线、面，并设置控件的可视性
        private void SymbolSelectDialog_Load(object sender, EventArgs e)
        {
            // 取得ArcGIS安装路径
            string sInstall = ESRI.ArcGIS.RuntimeManager.ActiveRuntime.Path;

            // 载入ESRI.ServerStyle文件到SymbologyControl
            this.axSymbologyControl.LoadStyleFile(sInstall + "Styles\\ESRI.ServerStyle");

            // 确定图层的类型（点、线、面），设置SymbologyControl的StyleClass，设置控件的可见性
            IGeoFeatureLayer geoFeatureLayer = m_layer as IGeoFeatureLayer;
            switch ((m_layer as IFeatureLayer).FeatureClass.ShapeType)
            {
                case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint:
                    this.setFeatureClassStyle(esriSymbologyStyleClass.esriStyleClassMarkerSymbols);
                    this.numericUpDownAngle.Enabled = true;
                    this.labelAngle.Enabled = true;
                    this.numericUpDownSize.Enabled = true;
                    this.labelSize.Enabled = true;
                    this.numericUpDownWidth.Enabled = false;
                    this.labelWidth.Enabled = false;
                    this.buttonFrameColor.Enabled = false;
                    this.labelFrameColor.Enabled = false;
                    break;
                case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline:
                    this.setFeatureClassStyle(esriSymbologyStyleClass.esriStyleClassLineSymbols);
                    this.numericUpDownAngle.Enabled = false;
                    this.labelAngle.Enabled = false;
                    this.numericUpDownSize.Enabled = false;
                    this.labelSize.Enabled = false;
                    this.numericUpDownWidth.Enabled = true;
                    this.labelWidth.Enabled = true;
                    this.buttonFrameColor.Enabled = false;
                    this.labelFrameColor.Enabled = false;
                    break;
                case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon:
                case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryMultiPatch:
                    this.setFeatureClassStyle(esriSymbologyStyleClass.esriStyleClassFillSymbols);
                    this.numericUpDownAngle.Enabled = false;
                    this.labelAngle.Enabled = false;
                    this.numericUpDownSize.Enabled = false;
                    this.labelSize.Enabled = false;
                    this.numericUpDownWidth.Enabled = true;
                    this.labelWidth.Enabled = true;
                    this.buttonFrameColor.Enabled = true;
                    this.labelFrameColor.Enabled = true;
                    break;
                default:
                    this.Close();
                    break;
            }
        }

        private void ButtonOk_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;

            // 取得选定的符号
            m_symbol = m_styleGalleryItem.Item as ISymbol;
            // 更新预览图像
            m_symbolImage = this.pictureBoxPreview.Image;
            // 关闭窗体
            this.Close();
        }

        private void ButtonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void AxSymbologyControl_OnMouseDown(object sender, ISymbologyControlEvents_OnMouseDownEvent e)
        {
            this.buttonOk.PerformClick();
        }

        private void previewImage()
        {
            stdole.IPictureDisp picture = this.axSymbologyControl.GetStyleClass(
                this.axSymbologyControl.StyleClass
            ).PreviewItem(m_styleGalleryItem, this.pictureBoxPreview.Width, this.pictureBoxPreview.Height);
            System.Drawing.Image image = System.Drawing.Image.FromHbitmap(new System.IntPtr(picture.Handle));
            this.pictureBoxPreview.Image = image;
        }

        private void axSymbologyControl_OnStyleClassChanged(object sender, ISymbologyControlEvents_OnStyleClassChangedEvent e)
        {
            switch (((ISymbologyStyleClass)e.symbologyStyleClass).StyleClass)
            {
                case esriSymbologyStyleClass.esriStyleClassMarkerSymbols:
                    this.numericUpDownAngle.Enabled = true;
                    this.labelAngle.Enabled = true;
                    this.numericUpDownSize.Enabled = true;
                    this.labelSize.Enabled = true;
                    this.numericUpDownWidth.Enabled = false;
                    this.labelWidth.Enabled = false;
                    this.buttonFrameColor.Enabled = false;
                    this.labelFrameColor.Enabled = false;
                    break;
                case esriSymbologyStyleClass.esriStyleClassLineSymbols:
                    this.setFeatureClassStyle(esriSymbologyStyleClass.esriStyleClassLineSymbols);
                    this.numericUpDownAngle.Enabled = false;
                    this.labelAngle.Enabled = false;
                    this.numericUpDownSize.Enabled = false;
                    this.labelSize.Enabled = false;
                    this.numericUpDownWidth.Enabled = true;
                    this.labelWidth.Enabled = true;
                    this.buttonFrameColor.Enabled = false;
                    this.labelFrameColor.Enabled = false;
                    break;
                case esriSymbologyStyleClass.esriStyleClassFillSymbols:
                    this.setFeatureClassStyle(esriSymbologyStyleClass.esriStyleClassFillSymbols);
                    this.numericUpDownAngle.Enabled = false;
                    this.labelAngle.Enabled = false;
                    this.numericUpDownSize.Enabled = false;
                    this.labelSize.Enabled = false;
                    this.numericUpDownWidth.Enabled = true;
                    this.labelWidth.Enabled = true;
                    this.buttonFrameColor.Enabled = true;
                    this.labelFrameColor.Enabled = true;
                    break;

            }
        }

        private void axSymbologyControl_OnItemSelected(object sender, ISymbologyControlEvents_OnItemSelectedEvent e)
        {
            m_styleGalleryItem = e.styleGalleryItem as IStyleGalleryItem;
            previewImage();
        }


    }
}
